package com.uam.core.lk.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Version 1.0
 * @Author 祝展
 * @Date 2019/10/25 14:39
 **/
public class MapUtils {

    /**
     * map转为list 加key加value
     */
    public static List<Object> map2List(Map<String, ?> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        List<Object> list = new ArrayList<Object>();
        for (Map.Entry<String, ?> entry : map.entrySet()) {
            list.add(entry.getKey());
            list.add(entry.getValue());
        }
        return list;
    }

    /**
     * 从map中获得long
     */
    public static long getLong4Map(Map<String, ?> map, String key) {
        return Long.parseLong(String.valueOf(map.get(key)));
    }

    /**
     * 移除map中的空值  空值判断 null ""
     */
    public static void removeEmptyValue(Map<String, ?> map) {
        if (null == map || map.isEmpty()) {
            return;
        }
        for (Map.Entry<String, ?> entry : map.entrySet()) {
            if (isEmpty(entry.getValue())) {
                map.remove(entry.getKey());
            }
        }
    }

    /**
     * 将map中value为空的去掉
     * @return
     */
    public static Map<String, ?> removeNull(Map<String, ?> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        Iterator<? extends Map.Entry<String, ?>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            if (null == it.next().getValue()) {
                it.remove();
            }
        }
        return map;
    }

    /**
     * 将Map的Object值转化为String
     */
    public static Map<String, String> obj2Str(Map<String, Object> map) {
        if (null == map || map.isEmpty()) {
            return null;
        }
        Map<String, String> res = new HashMap<String, String>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            res.put(entry.getKey(), String.valueOf(entry.getValue()));
        }
        return res;
    }

    /**
     * 将Map不为null的Object值转化为String
     */
    public static Map<String, String> obj2StrNotNull(Map<String, Object> map) {
        if (null == map || map.isEmpty()) {
            return null;
        }
        Map<String, String> res = new HashMap<String, String>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (null != entry.getValue()) {
                res.put(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }
        return res;
    }

    /**
     * 初始化Map
     */
    public static Map<String, String> initMap(String... args) {
        Map<String, String> map = new HashMap<String, String>();
        if (args.length == 0) {
            return map;
        }
        int size = args.length % 2 == 0 ? args.length : args.length - 1;
        for (int i = 0; i < size;) {
            map.put(args[i++], args[i++]);
        }
        return map;
    }

    public static Map<String, Object> getMap(Object... params) {
        if (null == params || params.length == 0) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int size = (params.length % 2 == 0 ? params.length : params.length - 1);
        for (int i = 0; i < size; i = i + 2) {
            map.put(String.valueOf(params[i]), params[i + 1]);
        }
        return map;
    }

    /**
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @param params 需要排除的key字段
     * @return 拼接后字符串
     */
    public static String map2String(Map<String, String> params, String exclude) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        StringBuffer prestr = new StringBuffer();
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            if (exclude.equals(key) || null == params.get(key)) {
                continue;
            }
            prestr.append("&" + key + "=" + params.get(key));
        }
        return prestr.substring(1);
    }

    private static boolean isEmpty(Object object) {
        if (null == object) {
            return true;
        }
        if (object instanceof String) {
            return "".equals(((String) object).trim());
        } else if (object instanceof Map) {
            return ((Map<?, ?>) object).isEmpty();
        } else if (object instanceof Collection) {
            return ((Collection<?>) object).isEmpty();
        }
        return false;
    }

    private MapUtils() {
    }
}
